/**
配置路由
*/
import { getToken } from '@/utils/token.js'
import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from '@/views/layout/Index.vue'
import login from '@/views/login/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    
    component: login
  },
  {
    path: '/layout',
    component: layout
  },
  // 从定向
  {
    path: '/',
    redirect: 'login'
  }
]
const router = new VueRouter({
  routes
})

//导航守卫

router.beforeEach((to,from,next)=>{
  if (to.path === '/login') {
    next()
  }else {

    const token = getToken()
    
    if(token){
      
      next()
    }else {
      next('/login')
    }
  }
})

// ES6中默认导出路由
export default router
